﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
using Mindfor.Web.Config;

namespace Mindfor.Web.Modules.Users
{
	/// <summary>
	/// Contains model data for registration.
	/// </summary>
	public class RegistrationModel : IValidatableObject
	{
		/// <summary>
		/// Gets or sets user login.
		/// </summary>
		[DisplayName("Логин")]
		[StringLength(255, ErrorMessage="Логин должен быть не длиннее 255 символов")]
		[Remote("UserCheckLogin", ErrorMessage="Логин уже занят")]
		public string Login { get; set; }

		/// <summary>
		/// Gets or sets e-mail.
		/// </summary>
		[DisplayName("E-mail")]
		[StringLength(255, ErrorMessage="E-mail должен быть не длиннее 255 символов")]
		[RegularExpression(RegexConsts.EmailRegex, ErrorMessage="Введите корректный e-mail")]
		[Remote("UserCheckEmail", ErrorMessage="E-mail уже занят")]
		public string Email { get; set; }

		/// <summary>
		/// Gets or sets password.
		/// </summary>
		[DisplayName("Пароль")]
		[Required(ErrorMessage="Введите пароль")]
		public string Pwd { get; set; }

		/// <summary>
		/// Gets or sets password confirmation that must match password.
		/// </summary>
		[DisplayName("Повтор пароля")]
		[Compare("Pwd", ErrorMessage="Повтор пароля не совпадает с паролем")]
		public string ConfirmPwd { get; set; }

		/// <summary>
		/// Determines whether the specified object is valid.
		/// </summary>
		public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			if (CmsConfig.Default.User.AuthenticateBy == AuthenticationType.Login && String.IsNullOrEmpty(Login))
				yield return new ValidationResult("Введите логин", new string[] { "Login" });

			if (CmsConfig.Default.User.IsEmailRequired && String.IsNullOrEmpty(Email))
				yield return new ValidationResult("Введите e-mail", new string[] { "Email" });
		}
	}
}
